package com.suwenhao.controller;


import com.alibaba.fastjson.JSONObject;
import com.suwenhao.constants.Constant;
import com.suwenhao.entity.Meeting;
import com.suwenhao.entity.User;
import com.suwenhao.enums.MeetingStatusEnum;
import com.suwenhao.service.MeetingService;
import com.suwenhao.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * @Author:suwenhao
 * @Description: 测试
 * @Date: Created in 2020/9/22 17:35
 */

@WebServlet("/meeting/*")
public class MeetingServlet extends BaseServlet {

    private MeetingService meetingService = new MeetingService();

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //数据
        List<Meeting> list = meetingService.listPage();

        req.setAttribute("list", list);

        req.getRequestDispatcher("/html/meeting/list.jsp").forward(req, resp);

    }

    protected void addMeeting(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, String[]> map = req.getParameterMap();

        Meeting meeting = new Meeting();

        try {
            BeanUtils.populate(meeting, map);
            meetingService.addMeeting(meeting);
        } catch (Exception e) {
            e.printStackTrace();
        }

        req.getRequestDispatcher("meeting/list").forward(req, resp);
    }


    protected void getMeetingById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, String> map = new HashMap<>();

        HttpSession session = req.getSession();

        Integer loginUserId = ((User) session.getAttribute(Constant.SESSION_LOGIN)).getId();

        String id = req.getParameter("id");

        Meeting meeting = meetingService.getMeetingById(Integer.valueOf(id));

        //所有参加会议的人数
        List<Integer> list = meetingService.listUserId(Integer.valueOf(id));

//        应到人数
        String makeUser = meeting.getMakeUser();
        Integer should = makeUser.split(",").length;
        map.put("should", should.toString());

        //实到人数
        if (list == null) {
            map.put("ShouldBeBTto", "0");
        } else {
            map.put("ShouldBeBTto", String.valueOf(list.size()));
//            map.put("ShouldBeBTto",list.size()+"");
        }

        //判断是否需要参加会议
        boolean b = makeUser.contains(loginUserId.toString());

        if (b) {
            //需要参加会议
            if (list.contains(loginUserId)) {
                //已参加会议
                map.put("flag", "1");
            } else {
                //未参加会议
                map.put("flag", "2");
            }

        } else {
            //不需要参加会议
            map.put("flag", "0");

        }

        req.setAttribute("map", map);


        req.setAttribute("meeting", meeting);

        req.getRequestDispatcher("/html/meeting/detail.jsp").forward(req, resp);

    }

    protected void attendTheMeeting(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        Meeting meetingById = meetingService.getMeetingById(Integer.valueOf(id));

        User user = new User();

        if (meetingById.getStatus().equals(MeetingStatusEnum.NOT_AT_THE.getValue())) {
            meetingService.attendTheMeeting(Integer.valueOf(id),user.getId());
        } else {

        }

        req.setAttribute("id", id);
        req.getRequestDispatcher("meeting/getMeetingById").forward(req, resp);
    }

}


